﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />

//*** 删除 Begin ***//
myapp.Index.deleteGroup_execute = function (screen) {
    screen.getGroupSet().then(function (groups) {
        // Delete selected
        groups.deleteSelected();

        // Save changes
        myapp.commitChanges().then(null, function fail(e) {
            // If error occurs, show the error.
            msls.showMessageBox(e.message, { title: e.title }).then(function () {
                // Cancel Changes
                myapp.cancelChanges();
                throw e;
            });
        });
    });
};
//*** 删除 End ***//

myapp.Index.VisitSite_execute = function (screen) {
    screen.getSiteCollection().then(function (sites) {
        window.open('http://' + window.location.hostname + ':' + sites.selectedItem.Port + '/Index.aspx');
    });
};

myapp.Index.ShowDownload_execute = function (screen) {
    screen.showPopup("SelectReg");
};

myapp.Index.DownloadReg_execute = function (screen) {
    screen.getQueryBySiteID().then(function (dbInfos) {
        var dbInfo = dbInfos.selectedItem;
        var regName = localStorage[dbInfo.Id];

        // This function will be wrapped in a Promise object
        function CallGetUserName(operation) {
            $.ajax({
                url: '../web/DownLoadReg.ashx',
                type: 'GET',
                cache: false,
                data: {
                    "Server": dbInfo.Server,
                    "Port": dbInfo.Port,
                    "DBName": dbInfo.DBName,
                    "UserName": dbInfo.UserName,
                    "Password": dbInfo.Password,
                    "FileName": dbInfo.Explanation,
                    "RegName": regName
                },
                success: operation.code(function AjaxSuccess(AjaxResult) {
                    operation.complete(AjaxResult);
                })
            });
        };

        if (!regName) {
            if (confirm("尚未配置站点路径信息，是否配置？\n确定后请选择对应的License文件")) {
                var file = document.createElement("input");
                file.type = "file";
                document.body.appendChild(file);

                file.addEventListener("change", handleFiles, false);
                function handleFiles() {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $(e.target.result).find("dbconns > dbconn").each(function (i, node) {
                            var $node = $(this);
                            if ($node.attr('appname') == 'Default') {
                                regName = $node.attr('regname');
                                localStorage[dbInfo.Id] = regName;
                            }
                        });

                        download();
                    };
                    reader.readAsText(file.files[0], "GB2312");

                    document.body.removeChild(file);
                }
                file.click();
            }
            else {
                download();
            }
        } else {
            download();
        }

        function download() {
            msls.promiseOperation(CallGetUserName).then(function PromiseSuccess(PromiseResult) {
                if (PromiseResult.indexOf('.reg') != -1) {
                    window.open(PromiseResult);
                }
            });
        }
    });
};


myapp.Index.ClearLocalStorage_execute = function (screen) {
    for (var s in localStorage) {
        localStorage.removeItem(s);
    }
};